package top.yamhk.agent.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @author 谢 YamHK-WebMvcConfigurerAdapter deprecated
 * @date 2019/4/14 19:03
 */
@Slf4j
@Component
//@Configuration
public class WebConfigOfServlet implements WebMvcConfigurer {
    /**
     * 拦截器的加载时间在Spring上下文之前加载
     * Configuration problem: @Bean method 'getLoginInterceptor' must not be private or final
     *
     * @return HandlerInterceptor
     */
    @Bean
    HandlerInterceptor getLoginInterceptor() {
        //return new LoginInterceptor();
        return new RequestInterceptor();
    }

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**").allowedHeaders("*").allowedMethods("*").allowedOrigins("*").allowCredentials(true).maxAge(1800);
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        //原来你是这样的路径?apk or swagger
        registry.addResourceHandler("/static/**")
                //2
                .addResourceLocations("classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/");
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //拦截所有
        registry.addInterceptor(getLoginInterceptor()).addPathPatterns("/**")
                //apk-依然会被拦截,因为此时变成了error这个路径
                .excludePathPatterns("/*.apk")
                .excludePathPatterns("/static/*");
        //registry.addInterceptor(new MvcThreadLocal.EnvWebMvcAdapter.RequestInterceptor()).addPathPatterns("/**");
        log.debug("拦截器添加成功");
    }

    private class RequestInterceptor implements HandlerInterceptor {
    }
    
}